package com.xianggu.reflect.demo;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class Demo {
    public void greet(String name) {
        System.out.println("Hello, " + name);
    }

    //调用实例方法
    public static void main(String[] args) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodType mt = MethodType.methodType(void.class, String.class);
        
        // 获取方法句柄
        MethodHandle mh = lookup.findVirtual(Demo.class, "greet", mt);
        
        // 调用方法（需绑定实例）
        Demo instance = new Demo();
        mh.invokeExact(instance, "Alice"); // 输出：Hello, Alice 
    }
}